package activities;

import com.iculture.FirstJsonExUrlActivity;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.matchcalendar.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class GlobalActivity extends FragmentActivity {
	private ImageButton buttonSlideMenu;
	private Button buttonSchedule, buttonMatch, buttonActivity, buttonShake, buttonFixedSchedule;
	private SlidingMenu slideMenu;
	
	protected Button getButtonSchedule() {
		return buttonSchedule;
	}
	
	protected Button getButtonMatch() {
		return buttonMatch;
	}
	
	protected Button getButtonActivity() {
		return buttonActivity;
	}
	
	protected Button getButtonShake() {
		return buttonShake;
	}
	
	protected void initGlobalView() {
		findView();
		setListenerOrAdapter();
	}

	private void findView() {
		slideMenu = new SlidingMenu(GlobalActivity.this);
		slideMenu.setMode(SlidingMenu.LEFT);
		slideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
		slideMenu.setShadowWidthRes(R.dimen.shadow_width);
		slideMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		slideMenu.setFadeDegree(0.35f);
		slideMenu.attachToActivity(GlobalActivity.this, SlidingMenu.SLIDING_CONTENT);
		slideMenu.setMenu(R.layout.custom_slidemenu);
		buttonSlideMenu = (ImageButton) findViewById(R.id.buttonSlideMenu);
		buttonMatch = (Button) findViewById(R.id.buttonMatch);
		buttonSchedule = (Button) findViewById(R.id.buttonSchedule);
		buttonActivity = (Button) findViewById(R.id.buttonActivity);
		buttonShake = (Button) findViewById(R.id.buttonShake);
		buttonFixedSchedule = (Button) findViewById(R.id.buttonFixedSchedule);
	}
	
	private void setListenerOrAdapter() {
		buttonSlideMenu.setOnClickListener(new ButtonSlideMenuOnClickListener());
		buttonMatch.setOnClickListener(new ButtonChangeFunctionClick(MatchActivity.class));
		buttonSchedule.setOnClickListener(new ButtonChangeFunctionClick(ScheduleActivity.class));
		buttonActivity.setOnClickListener(new ButtonChangeFunctionClick(FirstJsonExUrlActivity.class));
		buttonShake.setOnClickListener(new ButtonChangeFunctionClick(ShakeActivity.class));
		buttonMatch.setOnTouchListener(new ButtonChangeColorTouch());
		buttonSchedule.setOnTouchListener(new ButtonChangeColorTouch());
		buttonActivity.setOnTouchListener(new ButtonChangeColorTouch());
		buttonShake.setOnTouchListener(new ButtonChangeColorTouch());
		buttonFixedSchedule.setOnClickListener(new ButtonFixedScheduleClick());
		buttonFixedSchedule.setOnTouchListener(new BlackButtonChangeColorTouch());
	}
	
	private class ButtonSlideMenuOnClickListener implements ImageButton.OnClickListener {

		@Override
		public void onClick(View v) {
			slideMenu.toggle();
		}
	}
	
	private class ButtonChangeFunctionClick implements Button.OnClickListener {
		private Class<?> goToActivity;
		
		public ButtonChangeFunctionClick(Class<?> goToActivity) {
			this.goToActivity = goToActivity;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(GlobalActivity.this, goToActivity);
			Bundle bundle = new Bundle();
			
			bundle.putString("myFBID", getIntent().getExtras().getString("myFBID"));
			intent.putExtras(bundle);
			startActivity(intent);
			GlobalActivity.this.finish();
		}
	}
	
	private class ButtonChangeColorTouch implements Button.OnTouchListener {

		@Override
		public boolean onTouch(View view, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN)
				view.setBackgroundResource(R.color.blue4);
			else if (event.getAction() == MotionEvent.ACTION_UP)
				view.setBackgroundResource(R.color.gray4);
			return false;
		}
	}
	
	private class ButtonFixedScheduleClick implements Button.OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(GlobalActivity.this, SetFixedScheduleActivity.class);
			Bundle bundle = new Bundle();
			
			bundle.putString("myFBID", getIntent().getExtras().getString("myFBID"));
			intent.putExtras(bundle);
			startActivity(intent);
		}
	}
	
	private class BlackButtonChangeColorTouch implements Button.OnTouchListener {

		@Override
		public boolean onTouch(View view, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN)
				view.setBackgroundResource(R.color.blue4);
			else if (event.getAction() == MotionEvent.ACTION_UP)
				view.setBackgroundResource(R.color.black);
			return false;
		}
	}
}
